<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    //分页数
    protected $paginate = 10;

    /**
     * 返回json
     * @param int $error_code 状态码
     * @param array $data   返回内容
     * @param string $msg   提示信息
     * @param int $code
     * @return \Illuminate\Http\JsonResponse
     */
    public function witeJson($error_code=0,$data=[],$msg='',$code=200)
    {
        return response()->json([
            'error_code'=>$error_code,
            'data'=>$data,
            'msg'=>$msg
        ],$code);
    }

    /**
     * 子集递归
     * @param $data
     * @param int $pid
     * @return array
     */
    public function sonlevel($data,$pid=0)
    {
        $arr = [];
        foreach ($data as $key=>$val){

            if($val['pid'] == $pid){

                $val['son'] = $this->sonlevel($data,$val['id']);
                $arr[] = $val;
            }
        }
        return $arr;
    }

}
